package org.nnxy.foodboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.nnxy.foodboot.entity.UserRating;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户评分Mapper接口
 */
@Mapper
public interface UserRatingMapper extends BaseMapper<UserRating> {
    
    /**
     * 获取食谱的平均评分
     * @param recipeId 食谱ID
     * @return 平均评分
     */
    @Select("SELECT COALESCE(AVG(rating), 0) FROM user_rating " +
            "WHERE recipe_id = #{recipeId} AND del_flag = '0'")
    BigDecimal getAverageRating(@Param("recipeId") Long recipeId);
    
    /**
     * 获取用户对食谱的评分
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @return 用户评分
     */
    @Select("SELECT * FROM user_rating " +
            "WHERE user_id = #{userId} AND recipe_id = #{recipeId} AND del_flag = '0'")
    UserRating getUserRating(@Param("userId") Long userId, @Param("recipeId") Long recipeId);
    
    /**
     * 获取食谱的所有评价
     * @param recipeId 食谱ID
     * @return 评价列表
     */
    @Select("SELECT ur.*, u.username, u.nickname, u.avatar " +
            "FROM user_rating ur " +
            "LEFT JOIN user u ON ur.user_id = u.id " +
            "WHERE ur.recipe_id = #{recipeId} AND ur.del_flag = '0' " +
            "ORDER BY ur.create_time DESC")
    List<UserRating> getRecipeRatings(@Param("recipeId") Long recipeId);
} 